<template>
	<view style="height:100vh">
		<layout>
			<a-spin :spinning="spinning" style="width:100%"/>
			<a-card v-for="(item, index) in stockData" :key="index" :title="item.code" :bordered="true"
				class="stock-card">
				
				<a-descriptions size="small" :column="1">
					<a-descriptions-item label="业务类型">
						<a-tag :color="getTypeColor(item.type)">{{ item.type }}</a-tag>
					</a-descriptions-item>
					<a-descriptions-item label="当前状态">
						<a-tag :color="getStatusColor(item.status)">{{ item.status }}</a-tag>
					</a-descriptions-item>
					<a-descriptions-item label="操作人员">{{ item.operator }}</a-descriptions-item>
					<a-descriptions-item label="操作时间">{{ formatTime(item.time) }}</a-descriptions-item>
				</a-descriptions>

				<template #extra>
					<a-button type="link" size="small" @click="handleDetail(item)">详情</a-button>
				</template>
			</a-card>
		</layout>

	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import dayjs from 'dayjs';
	import {
		listInWarahouseApply4Audit,
		getInWarahouseApply,
		auditInWarahouseApply
	} from "@/api/wmsApply/inWarahouseApply";
	const stockData = ref([]);
	const spinning=ref(true);
	const getTypeColor = (type) => {
		const colors = {
			'采购申请': 'geekblue',
			'采购订单': 'purple',
			'采购合同': 'cyan'
		};
		return colors[type] || 'gray';
	};

	const getStatusColor = (status) => {
		const colors = {
			'已入库': 'green',
			'已审核': 'blue',
			'通过': 'volcano'
		};
		return colors[status] || 'gray';
	};

	const formatTime = (time) => {
		return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
	};

	const handleDetail = (item) => {
		// 处理详情点击事件
		console.log('查看详情:', item);
	};
	import {
		getQueryData
	} from "@/api/query/comQuery";
	import {
		getDicts
	} from "@/api/system/dict/data";
	async function getDictVaule(name, id) {
		try {
			const ref = await getDicts(name);
			let item = ref.data.find(item => item.dictValue === id) || null;
			return item.dictLabel;
		} catch (error) {
			console.error("获取字典项失败:", error);
			return null;
		}
	}

	async function getQuery(name, id) {
		try {
			const ref = await getQueryData(name);
			let item = ref.data.find(item => item.dictValue === id) || null;
			return item.dictLabel;
		} catch (error) {
			console.error("获取字典项失败:", error);
			return null;
		}
	}
	async function getList() {
		try {
			const ref = await listInWarahouseApply4Audit({
				pageNum: 1,
				pageSize: 100,
				applyStatus:2
			});
	
			console.log(ref);
			let d=[]
			// 使用 for...of 循环处理异步操作
			for (const item of ref.rows) {
				const admin = await getQuery("query_user", item.applyUser); // 等待 getQueryData 完成
				const status = await getDictVaule("apply_status", item.applyStatus); // 等待 getDictVaule 完成
	

				let a = {
					code: item.serialNo,
					type: '采购申请',
					operator: admin, // 使用 await 后的结果
					time: item.applyTime,
					status: status, // 使用 await 后的结果
				};
	
				d.push(a);
			}
			stockData.value=d;
			spinning.value=false;
			
		} catch (error) {
			console.error("获取列表失败:", error);
		}
	}
	getList()
</script>

<style scoped>
	/* 页面容器 */
	.container {
		min-height: 100vh;
		padding: 24rpx;
		background: linear-gradient(165deg, #f6fbf8, #e8f5f0);
	}

	/* 申请列表容器 */
	.requests-list {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(380rpx, 1fr));
		gap: 24rpx;
	}

	/* 申请卡片 */
	.request-card {
		background: linear-gradient(145deg, #ffffff, #f9fafb);
		border-radius: 16rpx;
		border: 1px solid rgba(39, 174, 96, 0.1);
		box-shadow: 0 8rpx 20rpx rgba(39, 174, 96, 0.08);
		overflow: hidden;
		transition: all 0.3s ease;
		animation: fadeIn 0.4s ease-out forwards;
		opacity: 0;
	}

	/* 为不同卡片添加延迟动画 */
	.request-card:nth-child(1) { animation-delay: 0.05s; }
	.request-card:nth-child(2) { animation-delay: 0.1s; }
	.request-card:nth-child(3) { animation-delay: 0.15s; }
	.request-card:nth-child(4) { animation-delay: 0.2s; }
	.request-card:nth-child(5) { animation-delay: 0.25s; }
	.request-card:nth-child(6) { animation-delay: 0.3s; }

	.request-card:hover {
		transform: translateY(-6rpx);
		box-shadow: 0 12rpx 30rpx rgba(39, 174, 96, 0.12);
	}

	/* 卡片头部 */
	.card-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 24rpx;
		background: linear-gradient(90deg, rgba(39, 174, 96, 0.08), rgba(46, 204, 113, 0.04));
		border-bottom: 1px solid rgba(39, 174, 96, 0.1);
	}

	.request-id {
		font-size: 32rpx;
		font-weight: 600;
		color: #2c3e50;
		display: flex;
		align-items: center;
	}

	.request-id::before {
		content: '';
		display: inline-block;
		width: 8rpx;
		height: 32rpx;
		background-color: #27ae60;
		border-radius: 4rpx;
		margin-right: 16rpx;
	}

	/* 状态标签 */
	.status-tag {
		padding: 6rpx 20rpx;
		border-radius: 30rpx;
		font-size: 24rpx;
		font-weight: 500;
		color: #ffffff;
	}

	.status-pending {
		background-color: #f39c12;
	}

	.status-approved {
		background-color: #27ae60;
	}

	.status-rejected {
		background-color: #e74c3c;
	}

	/* 卡片内容 */
	.card-body {
		padding: 24rpx;
	}

	.info-grid {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 16rpx;
	}

	.info-item {
		position: relative;
		padding-left: 10rpx;
		font-size: 28rpx;
		color: #4a6572;
		display: flex;
		align-items: center;
	}

	.info-item::before {
		content: '';
		position: absolute;
		left: 0;
		top: 50%;
		transform: translateY(-50%);
		width: 4rpx;
		height: 24rpx;
		background-color: #27ae60;
		border-radius: 2rpx;
	}

	.info-item:nth-child(1),
	.info-item:nth-child(2) {
		grid-column: span 2;
	}

	.info-label {
		font-weight: 500;
		color: #2c3e50;
		min-width: 140rpx;
		margin-right: 16rpx;
	}

	.info-value {
		flex: 1;
	}

	.info-highlight {
		color: #27ae60;
		font-weight: 600;
	}

	/* 卡片操作区 */
	.card-actions {
		display: flex;
		justify-content: flex-end;
		padding: 16rpx 24rpx;
		border-top: 1px solid rgba(39, 174, 96, 0.1);
		background-color: rgba(39, 174, 96, 0.02);
		gap: 16rpx;
	}

	/* 按钮样式 */
	:deep(.el-button--primary) {
		background-color: #27ae60 !important;
		border-color: #27ae60 !important;
		box-shadow: 0 4rpx 8rpx rgba(39, 174, 96, 0.2);
		transition: all 0.3s ease;
	}

	:deep(.el-button--primary:hover) {
		background-color: #219654 !important;
		transform: translateY(-2rpx);
		box-shadow: 0 6rpx 12rpx rgba(39, 174, 96, 0.25);
	}

	:deep(.el-button--danger) {
		background-color: #e74c3c !important;
		border-color: #e74c3c !important;
		box-shadow: 0 4rpx 8rpx rgba(231, 76, 60, 0.2);
		transition: all 0.3s ease;
	}

	:deep(.el-button--danger:hover) {
		background-color: #c0392b !important;
		transform: translateY(-2rpx);
		box-shadow: 0 6rpx 12rpx rgba(231, 76, 60, 0.25);
	}

	/* 响应式调整 */
	@media (max-width: 767px) {
		.requests-list {
			grid-template-columns: 1fr;
		}
		
		.info-grid {
			grid-template-columns: 1fr;
		}
		
		.info-item:nth-child(1),
		.info-item:nth-child(2) {
			grid-column: span 1;
		}
	}

	/* 动画 */
	@keyframes fadeIn {
		from { opacity: 0; transform: translateY(20rpx); }
		to { opacity: 1; transform: translateY(0); }
	}
</style>